/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import transfer.OrderState;

/**
 *
 * @author Sergey
 */
public class OrderStateBean implements EntityBean {
    private EntityContext entityContext;
    private DataSource dataSource;
    private OrderState orderState;

    public OrderState getOrderState() {
        return orderState;
    }

    public void setOrderState(OrderState orderState) {
        this.orderState = orderState;
    }
    
    public void setEntityContext(EntityContext entityContext) throws EJBException  {
        try {
            this.entityContext = entityContext;
            Context context = new InitialContext();
            dataSource = (DataSource) context.lookup("jdbc/ossystem");
        } catch (NamingException ex) {
            throw new EJBException("Does not find jdbc/ossystem");
        }
    }

    public void unsetEntityContext() {
        entityContext = null;
    }

    public void ejbRemove() throws RemoveException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(
                    "DELETE FROM Order_State WHERE id_order_state = ?");
            statement.setInt(1, orderState.getIdOrderState().intValue());
            if (statement.executeUpdate() < 1) {
                throw new RemoveException("Deleting error");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection, statement);

        }
    }

    public void ejbActivate() {
    }

    public void ejbPassivate()  {
    }

    public void ejbLoad() throws EJBException{
        Integer primary_key = (Integer) entityContext.getPrimaryKey();
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(
                    "SELECT id_order_state, name_state "
                    + "FROM Order_State WHERE id_order_state = ?");
            statement.setInt(1, primary_key.intValue());
            ResultSet resultSet = statement.executeQuery();
            if (!resultSet.next()) {
                throw new NoSuchEntityException("Role with id = " + primary_key + " not exists");
            }
            Integer idOrderState = new Integer(resultSet.getInt(1));
            String nameOrderState = resultSet.getString(2);
            orderState = OrderState.instance(idOrderState);
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            closeConnection(connection, statement);

        }
    }

    public void ejbStore() throws EJBException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(
                    "UPDATE Order_State SET name_state = ? "
                    + "WHERE id_order_state = ?");
            statement.setString(1, orderState.getNameOrderState());
            statement.setInt(2, orderState.getIdOrderState().intValue());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection, statement);
        }
    }
    public Collection ejbFindByNameState(String anameState) throws FinderException, EJBException {
        Connection connection = null;
        PreparedStatement statement = null;
        Collection list = new ArrayList() {};
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(
                    "SELECT id_order_state "
                    + "FROM Order_State WHERE name_state = ?");
            statement.setString(1, anameState);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Integer idOrderState = new Integer(resultSet.getInt(1));
                orderState = OrderState.instance(idOrderState);                 
                list.add(orderState.getIdOrderState());
            } 
            if (list.isEmpty()) {
                throw new FinderException("Order_States with this name is not exists");
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            closeConnection(connection, statement);

        }
    }
    
    public java.lang.Integer ejbFindByPrimaryKey(Integer aid_state) throws FinderException, EJBException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(
                    "SELECT id_order_state "
                    + "FROM Order_State WHERE id_order_state = ?");
            statement.setInt(1, aid_state.intValue());
            ResultSet resultSet = statement.executeQuery();
            if (!resultSet.next()) {
                throw new FinderException("Order_State with this id_state is not exists");
            }
            else {
                Integer idOrderState = new Integer(resultSet.getInt(1));
                orderState = OrderState.instance(idOrderState);
            }
            return orderState.getIdOrderState();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            closeConnection(connection, statement);

        }
    }

    private void closeConnection(Connection connection, Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
